package com.lwc.service.impl;

import com.lwc.bean.EnterpriseCategory;
import com.lwc.bean.EnterpriseTree;
import com.lwc.common.crud.BaseServiceImpl;
import com.lwc.common.exceotion.BusinessexCeption;
import com.lwc.mapper.EnterpriseCategoryMapper;
import com.lwc.service.EnterpriseCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

@Service
public class EnterpriseCategoryServiceImpl  extends BaseServiceImpl<EnterpriseCategory, EnterpriseCategoryMapper> implements EnterpriseCategoryService {

    @Autowired
    EnterpriseCategoryMapper enterpriseCategoryMapper;
    @Override
    public int save(EnterpriseCategory entity) {
     //根据父类别ID 查询最大编码
           String maxCode= enterpriseCategoryMapper.getMaxCodeById(entity.getParentId());
if (maxCode==null){
    //增加一级类别
    if (entity.getParentId()==0){
        entity.setCode("101");
    }else {
       //非一级类别 根据父类别id 查对应编码
       String tmp= baseMapper.getCodeById(entity.getParentId());
     entity.setCode(tmp+"01");
    }
}else {
    //存在的情况下
            int code=Integer.parseInt(maxCode);
            code++;
            entity.setCode(code+"");
        }

        return super.save(entity);
    }



    @Override
    public int delete(int id) {

        //根据id 判断数据库中是否有子类

        if (baseMapper.getistureByid(id)==0){
          baseMapper.delete(id);
        }else {
            throw new BusinessexCeption("该类中有子类请先删除子类");
        }



        return super.delete(id);
    }

    @Override
    public int update(int id, String name, int status) {
       return baseMapper.update(id,name,status);
    }

    @Override
    public List<EnterpriseCategory> querylist() {
   return baseMapper.querylist();
    }

    @Override
    public List<EnterpriseCategory> query() {

return  baseMapper.query();


    }

    @Override
    public List<EnterpriseTree> tree() {
return  baseMapper.tree(0);
    }


}
